package com.yaoweigroup.community.ui.base;

import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Common;
import com.yaoweigroup.community.config.Urls;
import com.yaoweigroup.community.util.DialogUtil;
import com.yaoweigroup.community.util.JsonUtil;
import com.yaoweigroup.community.util.LogUtil;
import com.yaoweigroup.community.util.UtilHttp;

/**
 * 修改密码
 * 
 * @author xi
 * 
 */
public class PassUpdateActivity extends Activity implements OnClickListener {

	private Button next_btn;
	private LinearLayout code_ll;
	private LinearLayout pass_ll;
	private RelativeLayout left_rl;

	private EditText phone_et;
	private EditText code_et;
	private TextView getCode_tv;
	private EditText pass_et;
	private EditText repass_et;
	private Button ok_btn;

	public final int CODE_SUCCESS = 0x2210;
	public final int CODE_FAIL = CODE_SUCCESS + 1;
	public final int RESET_SUCCESS = CODE_SUCCESS + 2;
	public final int RESET_FAILL = CODE_SUCCESS + 3;
	private Dialog dialog, regDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int width = this.getWindowManager().getDefaultDisplay().getWidth();
		Common.PROPORTION = width / 320f;
		Common.WIDTH = width;

		setContentView(R.layout.activity_pass_update);
		pass_et = (EditText) findViewById(R.id.pass_et);
		repass_et = (EditText) findViewById(R.id.repass_et);
		ok_btn = (Button) findViewById(R.id.ok_btn);
		ok_btn.setOnClickListener(this);

		phone_et = (EditText) findViewById(R.id.phone_et);
		code_et = (EditText) findViewById(R.id.code_et);
		getCode_tv = (TextView) findViewById(R.id.getCode_tv);
		getCode_tv.setOnClickListener(this);

		next_btn = (Button) findViewById(R.id.next_btn);
		next_btn.setOnClickListener(this);
		code_ll = (LinearLayout) findViewById(R.id.code_ll);
		pass_ll = (LinearLayout) findViewById(R.id.pass_ll);

		left_rl = (RelativeLayout) findViewById(R.id.left_rl);
		left_rl.setOnClickListener(this);
		dialog = DialogUtil.createWaitDialog(this, "获取验证码中..");
		regDialog = DialogUtil.createWaitDialog(this, "密码找回中..");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.next_btn:
			String codeStr = code_et.getText().toString();
			if (code != null && code.equals(codeStr)) {
				// code_ll.setVisibility(View.GONE);
				// pass_ll.setVisibility(View.VISIBLE);
				reset();
			} else {
				Toast.makeText(this, "验证码错误", 500).show();
			}
			break;
		case R.id.left_rl:
			finish();
			break;
		case R.id.getCode_tv:

			getCode();
			break;
		case R.id.ok_btn:
			reset();
			break;
		}
	}

	private String code = null;

	/**
	 * 获取验证码
	 */
	private void getCode() {

		dialog.show();
		String phoneStr = phone_et.getText().toString();
		if (phoneStr.length() == 0) {
			dialog.dismiss();
			Toast.makeText(this, "手机号不能为空!", 500).show();
			return;
		}
		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();
		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.add("phone", phoneStr);
		jsonUtil.add("flag", "findpwd");
		LogUtil.d("wxf", jsonUtil.toString());
		StringEntity enity = null;
		try {
			enity = new StringEntity(jsonUtil.toString());
		} catch (Exception e) {
		}
		params.setContentType("application/json");
		params.setBodyEntity(enity);
		params.setHeader("Accept", "application/json");
		http.send(HttpRequest.HttpMethod.POST, Urls.getAuthCode, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						LogUtil.d("wxf", "result = " + responseInfo.result);
						try {
							JSONObject obj = new JSONObject(responseInfo.result);
							int state = obj.getInt("state");
							String message = obj.getString("message");
							if (state == 200) {
								code = obj.getString("code");
								handler.sendEmptyMessage(CODE_SUCCESS);
							} else {
								Message msg = handler.obtainMessage();
								msg.what = CODE_FAIL;
								msg.obj = message;
								handler.sendMessage(msg);
							}

						} catch (Exception e) {
							handler.sendEmptyMessage(CODE_FAIL);
						}
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						LogUtil.e("wxf", error.toString());
						handler.sendEmptyMessage(CODE_FAIL);

					}

				});
	}

	/**
	 * 重重密码
	 */
	private void reset() {

		regDialog.show();

		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();
		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.add("username", phone_et.getText().toString());
		jsonUtil.add("phone", phone_et.getText().toString());
		LogUtil.d("wxf", jsonUtil.toString());
		StringEntity enity = null;
		try {
			enity = new StringEntity(jsonUtil.toString());
		} catch (Exception e) {
		}
		params.setContentType("application/json");
		params.setBodyEntity(enity);
		params.setHeader("Accept", "application/json");
		http.send(HttpRequest.HttpMethod.POST, Urls.resetPass, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						LogUtil.d("wxf", "result = " + responseInfo.result);
						try {
							JSONObject obj = new JSONObject(responseInfo.result);
							int state = obj.getInt("state");
							String message = obj.getString("message");
							if (state == 200) {
								handler.sendEmptyMessage(RESET_SUCCESS);
							} else {
								Message msg = handler.obtainMessage();
								msg.what = RESET_FAILL;
								msg.obj = message;
								handler.sendMessage(msg);
							}
						} catch (Exception e) {
							handler.sendEmptyMessage(RESET_FAILL);
						}
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						LogUtil.e("wxf", error.toString());
						handler.sendEmptyMessage(RESET_FAILL);
					}

				});

	}

	private String error = "错误信息";
	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case CODE_FAIL:
				dialog.dismiss();
				error = "获取验证码失败";
				if (msg.obj != null)
					error = msg.obj.toString();
				Toast.makeText(PassUpdateActivity.this, error, 500).show();
				break;
			case CODE_SUCCESS:
				dialog.dismiss();
				Toast.makeText(PassUpdateActivity.this, "验证码已发送", 500).show();
				break;
			case RESET_FAILL:
				regDialog.dismiss();
				error = "找回密码失败";
				if (msg.obj != null)
					error = msg.obj.toString();
				Toast.makeText(PassUpdateActivity.this, error, 500).show();
				break;
			case RESET_SUCCESS:
				regDialog.dismiss();
				Toast.makeText(PassUpdateActivity.this, "密码已发送手机，请查收", 500)
						.show();
				finish();
				break;
			}

		};
	};

	public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (pass_ll.getVisibility() == View.VISIBLE) {
				pass_ll.setVisibility(View.GONE);
				code_ll.setVisibility(View.VISIBLE);
			} else {
				finish();
			}
		}
		return super.onKeyDown(keyCode, event);
	};
}
